<template>
    <el-container v-if="isClassValid">
        <el-header>
            <div><img src="../../pictures/logo.png" width="68px" height="60px"> <span>科记：记录精彩校园生活</span></div>
            <el-menu
            :default-active="activeIndex2"
            class="el-menu-demo"
            mode="horizontal"
            background-color="#99ecff"
            text-color="black"
            active-text-color="#f59456">
                <el-menu-item class="grid-content" index="1">
                    <el-link href="#/home" target="_self" underline="false">科记主页</el-link>
                </el-menu-item>
                <el-submenu class="grid-content" index="3">
                    <template slot="title">生活助手</template>
                    <el-menu-item index="3-1">
                        <router-link :to="{ name: 'bill', params: { id: 1 } }">校园博客</router-link>
                    </el-menu-item>
                    <el-menu-item index="3-2">
                        <router-link :to="{ name: 'bill', params: { id: 2 } }">生活分享</router-link>
                    </el-menu-item>
                    <el-menu-item index="3-3"><a href="#/question" target="_self">校园问答</a></el-menu-item>
                </el-submenu>
                <el-submenu class="grid-content" index="4">
                    <template slot="title">班级事务</template>
                    <el-menu-item index="4-1">
                        <router-link :to="{ name: 'bill'}">班级通知</router-link>
                    </el-menu-item>
                    <el-menu-item index="4-2"><a href="#/account" target="_self">班级账目</a></el-menu-item>
                </el-submenu>
                <el-menu-item class="grid-content" index="5">
                    <el-link href="#/information" target="_self" underline="false">个人信息</el-link>
                </el-menu-item>
                <el-menu-item class="grid-content">
                    <div display: center>
                        <el-input placeholder="请输入内容" v-model="input3" class="input-with-select">
                            <el-select v-model="select" slot="prepend" placeholder="请选择">
                            <el-option label="校园博客" value="1"></el-option>
                            <el-option label="生活助手" value="2"></el-option>
                            <el-option label="校园问答" value="3"></el-option>
                            <el-option label="班级通知" value="4"></el-option>
                            </el-select>
                            <el-button slot="append" icon="el-icon-search"></el-button>
                        </el-input>
                    </div>
                </el-menu-item>
                <el-menu-item>
                    <div><el-col :span="12">
                            <div class="demo-basic--circle">
                                <div class="block"><el-avatar shape="square" :size="50" :src="squareUrl"></el-avatar></div>
                            </div>
                        </el-col> 
                    </div>
                </el-menu-item>
            </el-menu>
            <div class="line"></div>
        </el-header>
        
        <el-main>
            <el-card v-if="currentbill" class="bill-card">
                <h2 class="bill-title">{{ currentbill.eventName }}</h2>
                <p class="bill-author">发布者: <strong>{{ currentbill.coordinator }}</strong></p>
                <p class="bill-date"><em>{{ currentbill.eventDate }}</em></p>
                <div class="bill-details">
                    <p><strong>金额:</strong> <span class="bill-amount">{{ currentbill.amount }}</span></p>
                    <p><strong>经费来源:</strong> <span class="bill-fund-source">{{ currentbill.fundSource }}</span></p>
                </div>
                <div class="bill-content">
                    <p>{{ currentbill.content }}</p>
                </div>
                <el-button type="primary" @click="goback" class="back-button">返回</el-button>
            </el-card>
            

            <div v-else>
                <el-table
            :data="tableData"
            height="250"
            border
            style="width: 100%">

                <el-table-column
                prop="eventDate"
                label="日期"
                width="180">
                <template slot-scope="scope">
                    <span>{{ scope.row.eventDate }}</span>
                </template>
                </el-table-column>

                <el-table-column
                prop="coordinator"
                label="发布人"
                width="180">
                <template slot-scope="scope">
                    <span >{{ scope.row.coordinator }}</span>
                </template>
                </el-table-column>

                <el-table-column
                prop="totalCost"
                label="金额"
                width="180">
                <template slot-scope="scope">
                    <span>{{ scope.row.totalCost }}</span>
                </template>
                </el-table-column>

                <el-table-column
                prop="costSource"
                label="资金来源"
                width="180">
                <template slot-scope="scope">
                    <span>{{ scope.row.costSource }}</span>
                </template>
                </el-table-column>

                <el-table-column
                prop="eventName"
                label="活动名称">
                <template slot-scope="scope">
                    <span>{{ scope.row.eventName }}</span>
                </template>
                </el-table-column>

                <el-table-column
                    label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="more(scope.row.id)">修改</el-button>
                    </template>
                </el-table-column>
            </el-table>
            </div>
        </el-main>
        <el-footer>
            <el-row>
                <el-button type="primary" icon="el-icon-edit" circle @click="edit()"></el-button>
            </el-row>
        </el-footer>
    </el-container>
</template>

<script>

    import { getUsersinfo, getBillsinfo, getbillDetail } from '@/api/table.js'

    export default {
    data() {
      return {
        activeIndex: '1',
        activeIndex2: '1',//与字体颜色有关的值
        input: '',//搜索栏的输入
        select: '',//搜索栏的选择
        squareUrl: "https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png",//头像的访问路径
        isClassValid: false, // 用于判断班级信息是否有效
        currentbill: null, // 当前博客详情
        tableData: [],
      };
    },
    methods: {
        showbillDetail(billId) {// 根据博客ID请求博客详情
            const url = '/bills/single'; 
            getbillDetail(url, billId) // 发送请求获取博客详情
                .then(response => {
                    this.currentbill = response.data.data; // 将响应数据赋值给 currentbill
                })
                .catch(error => {
                    console.error('获取博客详情时出错:', error);
                });
        },

        goBack() {
            this.currentbill = null; // 返回到博客列表
        },

        fetchPaperInfo() {
            const url = '/bills/all';
            getBillsinfo(url) 
                .then(response => {
                    this.tableData = response.data.data; 
                })
                .catch(error => {
                    // 处理错误
                    console.error(error);
                });
        },

        fetchUserInfo() {
            getUsersinfo().then(response => {
                if(response.data.code === 0) {
                    alert('获取用户信息失败，请重新登录');
                    window.location.href = '#/login';
                    return false;
                } else { // 更新用户信息
                    const user = response.data.data;
                    if (user.classCode) { 
                        this.isClassValid = true; // 如果班级信息不为空，则允许显示页面
                        this.form.classCode = user.classCode;
                    } else {
                        this.isClassValid = false; // 否则不允许显示页面
                        alert('请先完善个人信息再访问该页面');
                    }
                    if (user.imageUrl) {
                        this.squareUrl = user.imageUrl;
                    }
                } 
            });
        },
        
        edit(){
            var targetUrl = "#/editclass";
            // 使用window.location.href进行页面跳转
            window.location.href = targetUrl;
        }
    },

    mounted() {
        this.fetchUserInfo();
        this.fetchPaperInfo();
    }
  }
</script>

<style>
    .el-header{
        background: #99ecff;
        display: flex;
        padding-left: 0;
        justify-content: space-between;
        color: black;
        font-size: 20px;
        > div{
            display: flex;
            align-items: center;
            span{
                margin-left: 15px;
            }
        }
    }
    .el-select .el-input {
        width: 130px;
    }
    .input-with-select .el-input-group__prepend {
        background-color: #fff;
    }
    .bill-card {
        border-radius: 10px;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        padding: 20px;
        margin: 20px 0;
    }
    
    .bill-title {
        font-size: 24px;
        font-weight: bold;
        margin-bottom: 10px;
        color: #333;
    }
    
    .bill-author {
        font-size: 16px;
        color: #555;
    }
    
    .bill-date {
        font-size: 14px;
        color: #999;
        margin-bottom: 15px;
    }
    
    .bill-content {
        font-size: 16px;
        line-height: 1.5;
        color: #444;
    }
    
    .back-button {
        margin-top: 20px;
    }

    .bill-amount {
        color: #f56c6c; /* 使金额醒目 */
        font-weight: bold;
    }
    
    .bill-fund-source {
        color: #409eff; /* 使经费来源醒目 */
    }
    .bill-details {
        font-size: 16px;
        color: #444;
        margin-bottom: 15px;
    }
    
</style>